<?php
namespace app\api\controller;

use app\common\controller\ApiBase;
use app\index\model\Article as ArticleModel;
use app\index\model\Category as CategoryModel;
use app\common\controller\HomeBase;
use think\Db;


class Index extends ApiBase {


    protected $category_model;
    protected $article_model;


    protected function _initialize() {
        
       // \think\Url::root('index.php'); 

       
        parent::_initialize();
        $this->article_model  = new ArticleModel();
        $this->category_model = new CategoryModel();
      
        $category_level_list  = $this->category_model->getLevelList();

        $this->assign('category_level_list', $category_level_list);  

    }

    /**
     * 获取轮播图
     */
    public function getSlideImage()
    {
        $list = $this->article_model->order("reading desc")->field("id,title,thumb")->limit(3)->select();
        foreach ( $list as $k=>$v ) {
            $list[$k]['thumb'] = APP_URL.$v['thumb'];
        }
        $this->json_result($list);
    }

    /**
     * 获取导航
     */
    public function getNav()
    {
        $list = $this->category_model->field("id,name")->select();
        $this->json_result($list);
    }

    /**
     * 获取文章列表
     */
    public function index() {
        $type  =  input("get.t_id");
        $type == 0? $where="":$where="cid=".$type;
        $perPage = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 10;
        $curPage = !empty($_GET['page']) ? intval($_GET['page']) : 1;
        $list = $this->article_model->where($where)->order("create_time desc")->limit(($curPage-1)*$perPage,$perPage)->select();
        foreach ( $list as $key=>$value ) {
            $list[$key]['content'] =strip_tags($value['content'],'');
            $list[$key]['content'] =str_replace("&nbsp;",'',$list[$key]['content']);
        }
        $this->json_result($list);
    }

    /**
     * 获取详情文章
     */
    public function detail()
    {
        $id  =  input("get.id");
        $detail = $this->article_model->where("id=".$id)->find();
        $path = APP_URL."/public/uploads";
        $detail['content'] = str_replace("/public/uploads",$path,$detail['content']);
        $this->json_result($detail);
    }


}
